package com.itheima.pinda;

import com.alibaba.fastjson.JSON;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.itheima.pinda.entity.AddressRule;
import com.itheima.pinda.service.DroolsRulesService;
import com.itheima.pinda.service.impl.DroolsRulesServiceImpl;
import com.itheima.pinda.utils.HttpUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class test {

    String ak = "LIOnvWxLn85bqkCRfLZHBbYYE9HoA9Ns";

    @Test
    public void test01() {

        HashMap<String, String> map = new HashMap<>();
        map.put("address", "浦东新区航头镇航都路18号");
        map.put("output", "json");
        map.put("ak", ak);

        String url = "https://api.map.baidu.com/geocoding/v3/";

        String str = null;
        try {
            str = HttpUtils.sendHttpGet(url, map);
        } catch (Exception e) {
            log.error("");
        }

        Map map1 = JSON.parseObject(str, Map.class);
        Map result = (Map) map1.get("result");
        Map location = (Map) result.get("location");


        String lng = new DecimalFormat("#.######").format(Double.parseDouble(location.get("lng").toString()));
        String lat = new DecimalFormat("#.######").format(Double.parseDouble(location.get("lat").toString()));
        System.out.println("lng = " + lng);
        System.out.println("lat = " + lat);
//        System.out.println("lng1 = " + location.get("lng").toString());
//        System.out.println("lat1 = " + location.get("lat").toString());
    }



    @Test
    public void test02(){

        HashMap<String, String> map = new HashMap<>();
        map.put("origin","31.0377,121.618625");
        map.put("destination","39.936404,116.452562");
        map.put("ak",ak);
        String url = "https://api.map.baidu.com/directionlite/v1/driving";

        String str = null;
        try {
            str = HttpUtils.sendHttpGet(url, map);
        } catch (Exception e) {
            e.printStackTrace();
        }

        Map map1 = JSON.parseObject(str, Map.class);

        Map result = (Map)map1.get("result");

        JSONArray routes = JSON.parseArray(result.get("routes").toString());

        JSONObject jsonObject = (JSONObject) routes.get(0);
        String distance = jsonObject.get("distance").toString();

        System.out.println("distance = " + distance);

    }

    @Test
    public void test03(){

        DroolsRulesServiceImpl rulesService = new DroolsRulesServiceImpl();

        AddressRule addressRule = new AddressRule();
        addressRule.setFirstFee(10);
        addressRule.setTotalWeight(2.5);
        addressRule.setContinuedFee(6.0);
        addressRule.setFirstWeight(1.0);
        String fee = rulesService.calcFee(addressRule);
        System.out.println("fee = " + fee);
    }

    @Test
    public void test0() {

        int age = 0;

        User user = new User();

        for (int i = 0; i < 10; i++) {
            user.setAge(age + "");
        }

        System.out.println("user = " + user);

    }
}

@Data
class User {
    String age;
}

